using System;
using System.Collections.Generic;
using System.Text;

namespace RikMigrations
{
	public enum ConstraintType
	{
		PrimaryKey,
		Unique
	}

	public class Constraint
	{
		public Constraint(string name, ConstraintType type, Table table, params string[] columns)
		{
			this.name = name;
			this.type = type;
			this.table = table;
			this.columns.AddRange(columns);
		}

		#region Properties
		private string name;
		public string Name
		{
			get { return name; }
			set { name = value; }
		}

		private ConstraintType type;
		public ConstraintType Type
		{
			get { return type; }
		}

		private Table table;

		public Table Table
		{
			get { return table; }
			set { table = value; }
		}

		private List<string> columns = new List<string>();
		public List<string> Columns
		{
			get { return columns; }
		}
		#endregion

		internal class AddConstraintCommand : Command
		{
			Constraint constraint;

			public AddConstraintCommand(Constraint constraint)
			{
				this.constraint = constraint;
			}

			public override void Execute(DbProvider db)
			{
				string sql = Providers.MssqlProvider.GetConstraintSql(constraint);
				db.ExecuteNonQuery("ALTER TABLE " + constraint.Table.Name + " ADD " + sql);
			}
		}

		internal class DropConstraintCommand : Command
		{
			Table table;
			string name;

			public DropConstraintCommand(Table table, string name)
			{
				this.table = table;
				this.name = name;
			}

			public override void Execute(DbProvider db)
			{
				// TODO: Remove any associated indices, constraints
				db.ExecuteNonQuery(string.Format("ALTER TABLE {0} DROP CONSTRAINT {1}", table.Name, name));
			}
		}
	}
}